using System;
using Microsoft.Xna.Framework;
using RebEngine.SceneObject;
using RebEngine.SceneObject.SceneGraph;
using Microsoft.Xna.Framework.Graphics;
using System.Collections.Generic;

// Attention: At the moment the whole scene is just stored in a 
// list. I would like to at least have a Octree Implemented for
// once we swap to working on the 3D portion. 

namespace RebEngine.Managers
{
    public class SceneGraphManager : GameComponent
    {

        private static bool _paused;
        public static int Occluded = 0;
        public static int Culled = 0;
        private static Node _root;
        private static QuadTree qTree;

        public static bool DebugDrawing { get; set; }

        #region Fields

        

        #endregion

        public static bool Paused
        {
            get { return _paused; }
            set { _paused = value; }
        }
        public static Node Root
        {
            get { return _root; }
        }

        public SceneGraphManager(Game game)
            : base(game)
        {
            Enabled = true;
            _root = new Node("BaseNode");
            qTree = new QuadTree(new Vector2(500, -500), new Vector2(1000, 1000), 1, 2);
        }

        public static void Draw(GameTime gameTime)
        {
            /*
            Matrix world = Matrix.CreateTranslation(0, 0, 0);

            Vector3 start = EngineManager.Device.Viewport.Unproject(new Vector3(0, 0, 0), CameraManager.ActiveCamera.Projection, CameraManager.ActiveCamera.View, world);
            Vector3 end = EngineManager.Device.Viewport.Unproject(new Vector3(EngineManager.Width, EngineManager.Height, 0), CameraManager.ActiveCamera.Projection, CameraManager.ActiveCamera.View, world);
            Rectangle test = new Rectangle((int)start.X, (int)start.Y, (int)(end.X - start.X), (int)(end.Y - start.Y));

            qTree.getObjectsInQuad(test);

            foreach(var temp in QuadTree.updateList)
            {
                temp.Draw(gameTime);
            }
            */
            _root.Draw(gameTime);            
        }

        public override void Update(GameTime gameTime)
        {
            _root.Update(gameTime);
        }

        public static void LoadContent()
        {
            _root.LoadContent();
        }

        public static void UnloadContent()
        {
            _root.UnloadContent();
        }

        public static void AddToTree(RebSceneObject newObject)
        {
            qTree.placeObject(newObject);
        }

        public static void AddObject(RebSceneObject newObject, string name)
        {
            SceneObjectNode node = new SceneObjectNode(newObject, name);
            _root.AddNode(node);
            AddToTree(newObject);
        }

        public static void AddObject(SceneObjectNode newObject)
        {
            _root.AddNode(newObject);
            AddToTree(newObject.SceneObject);
        }


        public static Node GetObject(string key)
        {
            return _root.GetNode(key);
        }

        public static void ClearAllObjects()
        {
            _root.UnloadContent();
            GC.Collect();
        }

        public static void ClearSelectedObject(RebSceneObject obj)
        {
            _root.UnloadContent(obj);
            GC.Collect();
        }

    }
}
